<?php

namespace App\Admin\Controllers\Circle;

use App\Admin\Repositories\Circle\WxCircleTag;
use App\Models\Circle\WxCircle;
use App\Models\Posts\WxTag;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxCircleTagController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxCircleTag(), function (Grid $grid) {
//            $grid->column('id')->sortable();
            $grid->quickSearch(['id', 'circle_id','tag_id'])->placeholder('搜索圈子ID，话题id...')->width(50);
            $grid->model()->orderBy('times', 'desc');
            $grid->column('circle_id')->sortable();
            $grid->column('circle_name', '圈子名称')->display(function ($v){
                return $v;
            });
            $grid->column('tag_id')->sortable();
            $grid->column('tag_name', '话题名称')->display(function ($v){
                return $v;
            });
            $grid->column('times')->editable()->sortable();
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
//                $filter->equal('id');
                $filter->equal('circle_id')->select(WxCircle::pluck('circle_name', 'id'));
            });
            $grid->disableActions();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxCircleTag(), function (Show $show) {
            $show->field('id');
            $show->field('circle_id');
            $show->field('tag_id');
            $show->field('times');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxCircleTag(), function (Form $form) {
            $form->display('id');
            $form->select('circle_id')->options(WxCircle::pluck('circle_name', 'id'));
            $form->select('tag_id', '话题')->options(function ($id) {
                if(!is_array($id) && $id) {
                    $tag = WxTag::find($id);
                    if (!_empty_($tag)) {
                        return [$tag->id => $tag->tags_name];
                    }
                }else{
                    return null;
                }
            })->ajax('select/tags');
            $form->text('times')->default(1);

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
